package com.share.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Station;
import com.share.device.domain.StationLocation;
import com.share.device.domain.StationVo;
import com.share.device.service.ICabinetService;
import com.share.device.service.IDeviceService;
import com.share.device.service.IStationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.domain.geo.Metrics;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private IStationService stationService;

    @Autowired
    private ICabinetService cabinetService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<StationVo> nearbyStation(String latitude, String longitude, Integer radius) {
        //确定中心点
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.parseDouble(longitude),Double.parseDouble(latitude));
        //设置查询半径
        Distance distance = new Distance(50, Metrics.KILOMETERS);
        //画圆；
        Circle circle = new Circle(geoJsonPoint, distance);

        //条件排除自己
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<StationLocation> stationLocationList = this.mongoTemplate.find(query,StationLocation.class);
        if(CollectionUtils.isEmpty(stationLocationList)) return null;
        //数据组装
        //提取StationId属性，转换成一个集合
        List<Long> stationIdList = stationLocationList.stream().map(StationLocation::getStationId).collect(Collectors.toList());
        //获取站点列表
        List<Station> stationList = stationService.list(new LambdaQueryWrapper<Station>().in(Station::getId,stationIdList).isNotNull(Station::getCabinetId));
//        //获取柜机的id列表；
//        //List<Long> cabibetIdList = stationList.stream().map(Station::getCabinetId).collect(Collectors.toList());
//        //获取柜机id与柜机信息map信息
//        //Map<Long, Cabinet> cabinetIdToMap = cabinetService.listByIds(cabibetIdList).stream().collect(Collectors.toMap(Cabinet::getId,Cabinet -> Cabinet));
        List<StationVo> stationVoList = new ArrayList<>();
        stationList.forEach(station -> {
            StationVo stationVo = new StationVo();
            BeanUtils.copyProperties(station,stationVo);
            Cabinet cabinet = cabinetService.getById(station.getCabinetId());
            //可用充电宝数量大于0，可借用
            if(cabinet.getAvailableNum() > 0) {
                stationVo.setIsUsable("1");
            }else{
                stationVo.setIsUsable("0");
            }
            //获取空闲插槽数量大于0，可归还；
            if(cabinet.getFreeSlots() > 0) {
                stationVo.setIsReturn("1");
            }else{
                stationVo.setIsReturn("0");
            }
            stationVoList.add(stationVo);
        });

        return stationVoList;

    }
}
